package leetcode.Hot100;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Cheng Jun
 * Description: 请输入描述
 * @version 1.0
 * @date 2021/12/13 20:59
 * 腾讯二面
 */
public class Trie {


    Set<String> wordSet = new HashSet<>();
    Set<String> prefixSet = new HashSet<>();

    public Trie() {
    }

    public void insert(String word) {
        if (!wordSet.contains(word)) {
            wordSet.add(word);
            if (!prefixSet.contains(word)) {
                for (int i = 1; i <= word.length(); i++) {
                    prefixSet.add(word.substring(0, i));
                }
            }
        }
    }

    public boolean search(String word) {
        return wordSet.contains(word);
    }

    public boolean startsWith(String prefix) {
        return prefixSet.contains(prefix);
    }
}
